/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN 
 */
package ws.y10k.goloim.model;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

import ws.y10k.goloim.util.HibernateUtil;

/**
 * @author natx
 *
 */
public class Pallets {
	
	Hashtable<String,Pallet> htPallets = new Hashtable<String,Pallet>();
	
	public Enumeration<Pallet> enumeration() {
		return htPallets.elements();
	}
	
	public void add(Pallet newPallet) throws PalletAlreadyExistsException {
		if ( htPallets.containsKey(newPallet.getCode()) ) throw new PalletAlreadyExistsException();
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			session.save(newPallet);
			transaction.commit();
			htPallets.put(newPallet.getCode(),newPallet);
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
	}
	
	public void add(String code, String description, double width, double height, double depth, double weight) throws PalletAlreadyExistsException {
		this.add(new Pallet(code,description,width,height,depth,weight));
	}
	
	public Pallet getPallet(String code) throws PalletDoesNotExistException {
		if ( !htPallets.containsKey(code) ) throw new PalletDoesNotExistException();
		return htPallets.get(code);
	}
	
	public boolean palletExists(String code) {
		return htPallets.containsKey(code);
	}

	public String[] getStringArray() {
		ArrayList<String> alPallets = new ArrayList<String>();

		Enumeration<Pallet> enuPallets = htPallets.elements();
		while ( enuPallets.hasMoreElements() ) {
			Pallet pallet = enuPallets.nextElement();
			alPallets.add(pallet.getCode());
		}
		String[] a = new String[alPallets.size()];
		alPallets.toArray(a);
		return a;
	}

	@SuppressWarnings("unchecked")
	public void load() {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			List<Pallet> pallets = session.createQuery("from Pallet").list();
			for ( Iterator<Pallet> itPallets = pallets.iterator(); itPallets.hasNext(); ) {
				Pallet pallet = itPallets.next();
				htPallets.put(pallet.getCode(),pallet);
			}
			transaction.commit();
		} catch (HibernateException e) {
			transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

}
